package com.example.library_hy_advert;

import android.content.Context;
import android.util.Log;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.example.library_hy_advert.db.DaoManager;
import com.example.library_hy_advert.system.Cache;
import com.example.library_hy_advert.system.Path;

import java.io.File;
import java.io.FileFilter;
import java.nio.file.Files;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;

public class HYAdvert {
    private static final String TAG = "HYAdvert";

    public static void init(Context context){
        Path.init();

        Cache.init();

        DaoManager.get().initDB(context,Path.getDatabasePath());

        //初始化日志配置
        LogUtils.getConfig().setLog2FileSwitch(Cache.getBoolean(Cache.Key.DEBUG));
        LogUtils.getConfig().setDir(Path.getLogPath());

        //清理日志目录
        List<File> logFileList = FileUtils
                .listFilesInDirWithFilter(
                        Path.getLogPath(),
                        pathname -> pathname.isFile() && pathname.lastModified() <= System.currentTimeMillis() - 7 * 24 * 60 * 60 * 1000
                );
        if (!logFileList.isEmpty()) {
            for (File file : logFileList) {
                FileUtils.delete(file);
            }
        }

        //清理Crash目录
        ThreadUtils.executeByIo(new ThreadUtils.SimpleTask<Boolean>() {
            @Override
            public Boolean doInBackground() throws Throwable {
                return FileUtils.deleteAllInDir(Path.getCrashDir());
            }

            @Override
            public void onSuccess(Boolean result) {
                FileUtils.delete(Path.getCrashDir());
            }
        });

    }

    public static int[] getImageTimeArray(Context context){
        return context.getResources().getIntArray(R.array.image_time);
    }

    public static int getImageTimeIndex() {
        return Cache.getInt(Cache.Key.IMAGE_TIME_INDEX,Cache.Default.IMAGE_TIME_INDEX);
    }

    public static void setImageTimeIndex(int index){
        Cache.setInt(Cache.Key.IMAGE_TIME_INDEX,index);
    }

    public static String getUserName() {
        return Cache.getString(Cache.Key.USER_NAME,Cache.Default.USER_NAME);
    }

    public static void setUserName(String userName){
        Cache.setString(Cache.Key.USER_NAME,userName);
    }


    public static String getPassword() {
        return Cache.getString(Cache.Key.PASSWORD,Cache.Default.PASSWORD);
    }

    public static void setPassword(String password){
        Cache.setString(Cache.Key.PASSWORD,password);
    }


    public static String getBusCode() {
        return Cache.getString(Cache.Key.BUS_CODE,Cache.Default.BUS_CODE);
    }

    public static void setBusCode(String busCode){
        Cache.setString(Cache.Key.BUS_CODE,busCode);
    }


}
